package com.cat.binaryTree;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/even-odd-tree/
 * @create 2025/9/29 17:02
 * @since JDK17
 */

public class Solution90 {
    public boolean isEvenOddTree(TreeNode root) {
        Deque<TreeNode> q = new ArrayDeque<>();
        q.add(root);
        for (int i = 0; !q.isEmpty(); i++) {
            List<Integer> list = new ArrayList<>();
            for (int j = 0, end = q.size(); j < end; j++) {
                var x = q.poll();
                list.add(x.val);
                if (x.val % 2 == i % 2) {
                    return false;
                }
                if (x.left != null) {
                    q.add(x.left);
                }
                if (x.right != null) {
                    q.add(x.right);
                }
            }
            for (int j = 1, end = list.size(); j < end; j++) {
                if (i % 2 == 0 && list.get(i) <= list.get(i - 1)) {
                    return false;
                }
                if (i % 2 == 1 && list.get(i) >= list.get(i - 1)) {
                    return false;
                }
            }
        }

        return true;
    }
}
